package com.lxmes.gs.service.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.lxmes.gs.domain.ProjectChanges;
import com.lxmes.gs.domain.WorkingHoursStandard;
import com.lxmes.gs.service.IProjectChangesService;
import com.lxmes.gs.service.IWorkingHoursStandardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.common.utils.bean.BeanValidators;
import com.lxmes.common.exception.ServiceException;
import javax.validation.Validator;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lxmes.gs.mapper.IssuedProjectsMapper;
import com.lxmes.gs.domain.IssuedProjects;
import com.lxmes.gs.service.IIssuedProjectsService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 项目下发Service业务层处理
 * 
 * @author wdf
 * @date 2023-04-24
 */
@Service
public class IssuedProjectsServiceImpl extends ServiceImpl<IssuedProjectsMapper,IssuedProjects> implements IIssuedProjectsService{
    
    @Autowired
    private IssuedProjectsMapper issuedProjectsMapper;
    @Autowired
    protected Validator validator;
    @Autowired
    private IProjectChangesService projectChangesService;
    @Autowired
    private IWorkingHoursStandardService workingHoursStandardService;
    /**
     * 查询项目下发
     * 
     * @param id 项目下发主键
     * @return 项目下发
     */
    @Override
    public IssuedProjects selectIssuedProjectsById(Long id){
        return issuedProjectsMapper.selectById(id);
    }

    /**
     * 查询项目下发列表
     * 
     * @param issuedProjects 项目下发
     * @return 项目下发
     */
    @Override
    public List<IssuedProjects> selectIssuedProjectsList(IssuedProjects issuedProjects){
        return issuedProjectsMapper.selectList(Wrappers.lambdaQuery(issuedProjects));
    }

    /**
     * 新增项目下发
     * 
     * @param issuedProjects 项目下发
     * @return 结果
     */
    @Override
    public int insertIssuedProjects(IssuedProjects issuedProjects){
        return issuedProjectsMapper.insert(issuedProjects);
    }

    /**
     * 修改项目下发
     * 
     * @param issuedProjects 项目下发
     * @return 结果
     */
    @Override
    public int updateIssuedProjects(IssuedProjects issuedProjects){
        return issuedProjectsMapper.updateById(issuedProjects);
    }

    /**
     * 批量删除项目下发
     * 
     * @param ids 需要删除的项目下发主键
     * @return 结果
     */
    @Override
    public int deleteIssuedProjectsByIds(Long[] ids){
        return issuedProjectsMapper.deleteIssuedProjectsByIds(ids);
    }

    /**
     * 删除项目下发信息
     * 
     * @param id 项目下发主键
     * @return 结果
     */
    @Override
    public int deleteIssuedProjectsById(Long id){
        return issuedProjectsMapper.deleteById(id);
    }
    @Override
    public String imporIteminfo(List<IssuedProjects> issuedProjectsList, boolean updateSupport) {
        if (StringUtils.isNull(issuedProjectsList) || issuedProjectsList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (IssuedProjects issuedProjects : issuedProjectsList)
        {
            try
            {
                BeanValidators.validateWithException(validator, issuedProjects);
                this.insertIssuedProjects(issuedProjects);
                successNum++;
                successMsg.append("<br/>" + successNum + "数据导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/> 数据导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int auditIssuedProjects(Long[] ids) {
        List<IssuedProjects> issuedProjects = baseMapper.selectBatchIds(Arrays.asList(ids));
        if (issuedProjects.stream().anyMatch(i -> StrUtil.equals(i.getAudit(), "1"))) {
            throw new ServiceException("存在已审核数据,审核下发失败");
        }
        for (IssuedProjects issuedProject : issuedProjects) {
            issuedProject.setAudit("1");
            issuedProject.setPlanStatus("1");
            baseMapper.updateById(issuedProject);
            
            ProjectChanges projectChanges = BeanUtil.copyProperties(issuedProject, ProjectChanges.class);
            projectChanges.setPlanStatus("1");
            projectChanges.setAudit("0");
            projectChangesService.insertProjectChanges(projectChanges);
            
            WorkingHoursStandard workingHoursStandard = BeanUtil.copyProperties(issuedProject, WorkingHoursStandard.class);
            workingHoursStandard.setProjectCode(issuedProject.getApprovalCode());
            workingHoursStandard.setProjectName(issuedProject.getApprovalName());
            workingHoursStandardService.insertWorkingHoursStandard(workingHoursStandard);
        }
        return 1;
    }
}
